package org.clever.arkdemo.video.utils;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangxingyu
 */
public class UrlParamMap {

    private final Map<String, String> paramMap;

    public UrlParamMap(Map<String, String> paramMap) {
        this.paramMap = paramMap;
    }
    public UrlParamMap() {
        this.paramMap = new HashMap<>();
    }

    public void putNotNull(String key, Object value) {
        if (value != null) {
            paramMap.put(key, String.valueOf(value));
        }
    }

    public String get(String key) {
        return paramMap.get(key);
    }

    public Map<String, String> getParamMap() {
        return paramMap;
    }

    public String toUrlParam() {
        StringBuilder sb = new StringBuilder();
        if (!paramMap.isEmpty()) {
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                String value = URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8);
                sb.append(entry.getKey()).append("=").append(value).append("&");
            }
            sb.setLength(sb.length() - 1);
        }
        return sb.toString();
    }
}
